package pers.mine.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysql.fabric.xmlrpc.base.Array;

import pers.mine.dao.GoodsGroupMapper;
import pers.mine.dao.GoodsItemMapper;
import pers.mine.dao.GroupShipMapper;
import pers.mine.dao.QueryMapper;
import pers.mine.po.GoodsGroup;
import pers.mine.po.GoodsItem;
import pers.mine.po.GroupShip;
import pers.mine.service.inter.GroupShipService;

/**
 * @author Mine
 * @since 2016年11月27日_下午8:53:06
 */
@Service
public class GroupShipServiceImpl implements GroupShipService {
	@Autowired
	private GroupShipMapper groupShipMapper;
	@Autowired
	private GoodsGroupMapper goodsGroupMapper;
	@Autowired
	private GoodsItemMapper goodsItemMapper;
	@Autowired
	private QueryMapper queryMapper;
	// 日志记录器
	private static final Log logger = LogFactory
			.getLog(GoodsGroupServiceImpl.class);

	@Override
	public boolean addGroupsForGoods(Integer goodsId, Integer[] groups) {
		// 先删除原有的
		if (delShipByGoodsId(goodsId)) {
			// 初始化List
			List<GroupShip> ships = null;
			if (groups != null) {
				ships = new ArrayList<GroupShip>();
				GroupShip temp = null;
				for (Integer groupId : groups) {
					if (groupId != null) {
						temp = new GroupShip();
						temp.setGoodsId(goodsId);
						temp.setGroupId(groupId);
						ships.add(temp);// 添加
					}
				}
				if (groupShipMapper.insertList(ships) != ships.size()) {
					if (logger.isDebugEnabled()) {
						logger.debug("添加指定商品 映射:失败!影响行数不科学。。。");
					}
				} else {
					return true;
				}
			}
		}
		if (logger.isDebugEnabled()) {
			logger.debug("添加指定商品 映射:失败!添加前的--删除id商品映射失败。。。");
		}
		return false;
	}

	@Override
	public boolean delShipByGoodsId(Integer goodsId) {
		// 参数非空判断
		if (goodsId == null) {
			logger.debug("删除指定商品所有映射:传入参数为空...");
			return false;
		}
		// 查询指定商品是否存在
		if (goodsItemMapper.selectByPrimaryKey(goodsId) != null) {
			// 执行 删除
			groupShipMapper.deleteByGoodsId(goodsId);
			return true;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("删除指定商品所有映射:失败!指定id商品不存在。。。");
		}
		return false;
	}

	@Override
	public boolean delShipByGroupId(Integer groupId) {
		// 参数非空判断
		if (groupId == null) {
			logger.debug("删除指定商品组所有映射:传入参数为空...");
			return false;
		}
		// 查询指定商品组是否存在
		if (goodsGroupMapper.selectByPrimaryKey(groupId) != null) {
			// 执行 删除
			groupShipMapper.deleteByGroupId(groupId);
			return true;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("删除指定商品组所有映射:失败!指定id商品组不存在。。。");
		}
		return false;
	}

	@Override
	public List<GoodsItem> findGoodsItemsByGroupId(Integer groupId) {
		// 参数非空判断
		if (groupId == null) {
			logger.debug("查询商品组id下属所有商品:传入参数为空...");
			return null;
		}
		// 使用查询DAO
		return queryMapper.findGoodsItemsByGroupId(groupId);
	}

	@Override
	public List<GoodsGroup> findGroupsByGoodsId(Integer goodsId) {
		// 参数非空判断
		if (goodsId == null) {
			logger.debug("查询指定id商品所属所有商品组:传入参数为空...");
			return null;
		}
		return queryMapper.findGroupsByGoodsId(goodsId);
	}

	@Override
	public List<GoodsItem> fillTypeForGoodsList(List<GoodsItem> goodsList) {
		List<GoodsItem> targetList = null;
		if (goodsList != null) {
			targetList = new ArrayList<GoodsItem>();
			List<GoodsGroup> types = null;
			for (GoodsItem goodsItem : goodsList) {
				// 获取id商品类型
				types = findGroupsByGoodsId(goodsItem.getId());
				// 格式化
				goodsItem.setType(this.getTypeStr(types));
				// 添加到目标List
				targetList.add(goodsItem);
			}
		}
		return targetList;
	}

	/**
	 * 格式化指定商品组List的类型名
	 * 
	 * @param types
	 * @return
	 */
	@Override
	public String getTypeStr(List<GoodsGroup> types) {
		String x = null;
		if (types != null) {
			StringBuilder temp = new StringBuilder();
			int size = types.size();
			for (int i = 0; i < size; i++) {
				temp.append(types.get(i).getName());
				if (i != (size - 1)) {
					temp.append("、");
				}
			}
			x = temp.toString();
		}
		if (x == null || "null".equalsIgnoreCase(x) || "".equals(x)) {
			x = "未指定类型";
		}
		return x.trim();
	}

}
